﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.Helpers
{
    public class LocalizationHelper
    {
        [Serializable]
        public class LocalizationSettings
        {
            #region Properties

            private CurrencyInfo _Currency;
            public CurrencyInfo Currency
            {
                get { return _Currency; }
                set { _Currency = value; }
            }

            #endregion

            public LocalizationSettings()
            {
                this.Currency = new CurrencyInfo();
            }

            [Serializable]
            public class CurrencyInfo
            {
                public string DecimalSeparator      = ".";
                public string ThousandsSeparator    = ",";
                public int DecimalPlaces            = 2;
                public string Symbol                = "u$s";
                public char ZeroValue               = '0';
                public string DefaultValue          = "0";
                public string SampleValue           = "0.00";
            }
        }

        #region Private Methods

        private static LocalizationSettings CreateLocalizationSettings(System.Globalization.CultureInfo cultureInfo)
        {
            var settings = new LocalizationSettings
            {
                Currency = new LocalizationSettings.CurrencyInfo()
                {
                    DecimalSeparator    = cultureInfo.NumberFormat.CurrencyDecimalSeparator,
                    ThousandsSeparator  = cultureInfo.NumberFormat.CurrencyGroupSeparator,
                    DecimalPlaces       = cultureInfo.NumberFormat.CurrencyDecimalDigits,
                    Symbol              = cultureInfo.NumberFormat.CurrencySymbol,
                    ZeroValue           = '0',
                    DefaultValue        = "0",
                    SampleValue         = (1000.1).ToString(cultureInfo)
                }
            };
            settings.Currency.DefaultValue = String.Format("{0}{1}{2}", settings.Currency.ZeroValue, settings.Currency.DecimalSeparator, "".PadRight(settings.Currency.DecimalPlaces, settings.Currency.ZeroValue));
            return settings;
        }

        #endregion

        #region Public Methods

        public static LocalizationSettings GetLocalizationSettings(System.Globalization.CultureInfo cultureInfo)
        {
            var key = String.Format("{0}_{1}", Common.WellKnownKeys.Session.CULTURE_INFO, cultureInfo.Name);
            var settings = Helpers.WebHelper.Session.Get<LocalizationSettings>(key);
            if (settings == null)
            {
                settings = CreateLocalizationSettings(cultureInfo); 
                Helpers.WebHelper.Session.Set(key, settings);
            }
            return settings;
        }

        #endregion

    }
}